package base.thread.GuiTuRuing;

/**
 * @Author: my_code
 * @Create: 2020-12-06 20:31
 */
public class Race implements Runnable {

    private static String winner;

    public static void main(String[] args) {

        Race race = new Race();
        new Thread(race, "tortoise").start();
        new Thread(race, "rabbit").start();

    }

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            boolean flag = gameOver(i);
            System.out.println(Thread.currentThread().getName() + "-->跑了" + i + "步");
            if (flag) {
                break;
            }
            /*if (Thread.currentThread().getName().equals("rabbit") && i%20==0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }*/
        }
    }

    private boolean gameOver(int steps) {
        if (winner != null) {
            return true;
        }

        if (steps >= 100) {
            winner = Thread.currentThread().getName();
            System.out.println("Winner: is " + winner);
            return true;
        }
        return false;
    }
}
